package meminin.model;

import com.mysql.jdbc.ResultSetMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class MySqlDataRecord implements DataRecord {

    public abstract String getInsertQuery(String dbName, String tableName);

    public abstract String getReadQuery(String dbName, String tableName);

    public abstract String getUpdateQuery(String dbName, String tableName, DataRecord newData);

    public abstract String getDeleteQuery(String dbName, String tableName);

    public static DataRecord createRecord(ResultSet records, DataRecord emptyTemplate) {
        ResultSetMetaData rsmd = null;
        DataRecord tempRecord = emptyTemplate.getCopy();
        try {
            rsmd = (ResultSetMetaData) records.getMetaData();
            int i, numColumns = rsmd.getColumnCount();
            for (i = 1; i <= numColumns; i++) {
                String columnName = rsmd.getColumnName(i);
                tempRecord.setAttributeValue(columnName, records.getString(columnName));
            }
        } catch (SQLException ex) {
            Logger.getLogger(MySqlDataRecord.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tempRecord;
    }
}
